package org.hhchat.leetcode.dp;

/**
 * Created this one by HMH on 2017/6/16.
 * leetcode解法
 * https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/#/solutions
 */
public class code309 {

    public static class Solution {
        public int maxProfit(int[] prices) {
            int sell = 0, pre_sell = 0, buy = Integer.MIN_VALUE, pre_buy;
            for (int p : prices) {
                pre_buy = buy;
                buy = Math.max(pre_sell - p, pre_buy);
                pre_sell = sell;
                sell = Math.max(pre_buy + p, pre_sell);
            }
            return sell;
        }
    }

    public static void main(String[] args){
        Solution solution = new Solution();
        System.out.println(solution.maxProfit(new int[]{
                1,2,3,0,2
        }));
    }

}
